package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.WmsProductVelocityMapper;
import com.jy.wms.pojo.WmsProductVelocity;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;



/**
 * 产品周转率报表
 * Created by syh on 2018/5/8.
 */
@Service
public class WmsProductVelocityService extends BaseService<WmsProductVelocityMapper,WmsProductVelocity> {
    /**
     * 分页查询
     * @param entity
     * @return
     * @throws ServiceException
     */
   @BaseServiceDescribe(moduleName = "产品周转率报表",btnName = "分页查询产品周转率报表")
   public Page<WmsProductVelocity> baseSelectPage(WmsProductVelocity entity) throws ServiceException {
       try{
           PageHelper.startPage(entity.getPage(),entity.getRows());
           getFalgTime(entity);
           return (Page<WmsProductVelocity>) dao.baseSelectPage(entity);
       }catch(Exception e){
           throw  new ServiceException("查询产品周转率报表失败",e);
       }
   }

    /**
     * 分页查询和导出提取 改时间格式的共同部分
     * @param entity
     * @throws ParseException
     */
    public void getFalgTime(WmsProductVelocity entity) throws ParseException {
        if (entity.getNytime()!=null && !("").equals(entity)){
            String date =entity.getNytime();//年月
            date = date.replace("Z", " UTC");//注意是空格+UTC
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
            Date d = format2.parse(date);
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM");//注意格式化的表达式
            entity.setNytime(format1.format(d).toString());
        }
    }

    /**
     * 导出查询
     * @param entity
     * @return
     */
   public List<WmsProductVelocity> getVelocityExport(WmsProductVelocity entity) throws ServiceException{
        try{
            getFalgTime(entity);
           return  dao.baseSelectPage(entity);
        }catch(Exception e){
            throw  new ServiceException("导出查询产品周转率报表失败",e);
        }
   }

    /**
     * 产品周转率报表 定时器查询信息
     * @throws Exception
     */
    @BaseServiceDescribe(moduleName = "产品周转率报表 定时器查询信息", btnName = "产品周转率报表信息")
    @Transactional
    public void getVelocityTimer() throws Exception{
        JSONObject json=getTime();
        //第一个方法:查所有产品种类
        List<WmsProductVelocity> list=dao.getVelocity();
        for(WmsProductVelocity wmsProductVelocity : list){
            //第二个:查询月出库支数,月出库箱数(传入第一天的开始和最后一天的结束时间,产品类型id)

            WmsProductVelocity wmsProductVelocity1= dao.getCkZsxs(wmsProductVelocity.getCplxid(), json.get("fysj_start").toString(),json.get("fysj_end2").toString());
            wmsProductVelocity.setId(UUIDUtils.uuid());
            wmsProductVelocity.setNytime(json.get("nytime").toString());
            wmsProductVelocity.setYckzs(wmsProductVelocity1.getYckzs());
            wmsProductVelocity.setYckxs(wmsProductVelocity1.getYckxs());
            //第三个:计算月出库托数{月出库支数/(TI*HI*箱主单位数量)};TI*HI为*时,月出库托数等于0
           Integer yckts=dao.getYckts(wmsProductVelocity1.getYckzs(),wmsProductVelocity.getCplxid());
            if(yckts==null){
                yckts = 0;
            }
            wmsProductVelocity.setYckts(yckts);
            //第四个:期初数量
            Integer ypjkczs1=dao.getYpjkczs(json.get("fysj_start").toString(),json.get("fysj_end").toString(),wmsProductVelocity.getCplxid());
           //第五个:期末数量
            Integer ypjkczs2=dao.getYpjkczs(json.get("fysj_start2").toString(),json.get("fysj_end2").toString(),wmsProductVelocity.getCplxid());
            //月平均库存支数(期末数量+期初数量)/2
            Integer ypjkczs=(ypjkczs1+ypjkczs2)/2;
            wmsProductVelocity.setYpjkczs(ypjkczs);
            double kczzl1=0;
            if(!ypjkczs.equals(0) && !"".equals(ypjkczs) && ypjkczs!=null){
                kczzl1=wmsProductVelocity1.getYckzs()/ypjkczs;
            }
            //计算库存周转率kczzl
            java.math.BigDecimal kczzl = new java.math.BigDecimal(kczzl1);
            wmsProductVelocity.setKczzl(kczzl);
            //第六个:储位周转率cwzzl(月出库托数/该品类规划储位)
            double cwzzl2=0;
            Integer cwsl=dao.getGhcw(wmsProductVelocity.getCplxid());
            if(!cwsl.equals(0) && !"".equals(cwsl) && cwsl!=null){
                cwzzl2=yckts/cwsl;
            }
            java.math.BigDecimal cwzzl = new java.math.BigDecimal(cwzzl2);
            wmsProductVelocity.setCwzzl(cwzzl);
            dao.baseInsert(wmsProductVelocity);
        }

    }
    /**
     *提取时间部分供定时器方法调用
     * @return
     */
    public JSONObject getTime() {
        JSONObject json = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        //获取前一个月的月日
        String nytime = sdf1.format(calendar.getTime());
        json.put("nytime",nytime);
        //获取前一个月第一天
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, -1);
        calendar1.set(Calendar.DAY_OF_MONTH,1);
        calendar1.set(Calendar.HOUR_OF_DAY, 00);
        calendar1.set(Calendar.MINUTE, 00);
        calendar1.set(Calendar.SECOND, 00);
        //fysj_start上个月第一天的开始时间
        String fysj_start = sdf.format(calendar1.getTime());
        json.put("fysj_start",fysj_start);
        calendar1.set(Calendar.HOUR_OF_DAY, 23);
        calendar1.set(Calendar.MINUTE, 59);
        calendar1.set(Calendar.SECOND, 59);
        //fysj_end上个月第一天的结束时间
        String fysj_end = sdf.format(calendar1.getTime());
        json.put("fysj_end",fysj_end);
        //获取前一个月最后一天
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.DAY_OF_MONTH, 0);
        calendar2.set(Calendar.HOUR_OF_DAY, 00);
        calendar2.set(Calendar.MINUTE, 00);
        calendar2.set(Calendar.SECOND, 00);
        //fysj_start2上个月最后一天的开始时间
        String fysj_start2 = sdf.format(calendar2.getTime());
        json.put("fysj_start2",fysj_start2);
        calendar2.set(Calendar.HOUR_OF_DAY, 23);
        calendar2.set(Calendar.MINUTE, 59);
        calendar2.set(Calendar.SECOND, 59);
        //fysj_end2上个月最后一天的结束时间
        String fysj_end2 = sdf.format(calendar2.getTime());
        json.put("fysj_end2",fysj_end2);
        return json;
    }

}